import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
import componentsRouter from './modules/components'
// import chartsRouter from './modules/charts'
// import nestedRouter from './modules/nested'

export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/dashboard/index'),
        name: 'Dashboard',
        meta: { title: '控制台', icon: 'dashboard', affix: true, auth: true }
      }
    ]
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [

  {
    path: '/home',
    component: Layout,
    redirect: 'noRedirect',
    name: 'home',
    meta: {
      title: '首页管理',
      icon: 'el-icon-s-home', auth: true
    },
    children: [
      {
        path: 'banner',
        component: () => import('@/views/home/banner'),
        name: 'banner',
        meta: { title: '轮播图', auth: true }
      },
      {
        path: 'muen',
        component: () => import('@/views/home/muen'),
        name: 'muen',
        meta: { title: '导航栏', auth: true }
      },
      {
        path: 'userInfo',
        component: () => import('@/views/home/userInfo'),
        name: 'userInfo',
        meta: { title: '个人信息', auth: true }
      }]
  },
  componentsRouter,
  {
    path: '/project',
    component: Layout,
    redirect: 'noRedirect',
    name: 'project',
    meta: {
      title: '项目管理',
      icon: 'component',
      auth: true
    },
    children: [
      {
        path: 'addProject',
        component: () => import('@/views/project/addProject'),
        name: 'addProject',
        meta: { title: '添加项目', auth: true }
      },
      {
        path: 'projectList',
        component: () => import('@/views/project/projectList'),
        name: 'projectList',
        meta: { title: '项目列表', auth: true }
      }
    ]
  },
  {
    path: '/message',
    component: Layout,
    children: [
      {
        path: 'index',
        component: () => import('@/views/message'),
        name: 'message',
        meta: { title: '留言管理', icon: 'el-icon-s-comment', auth: true
        }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
